package org.androidpn.server.xmpp.packet;

import java.util.ArrayList;
import java.util.List;

import org.androidpn.server.entity.FriendGroup;
import org.jivesoftware.smack.util.StringUtils;
import org.xmpp.packet.IQ;

/**
 * packet friendGroup
 * 
 * @author 蔡琪
 * 
 */
public class FriendGroupPacket extends IQ {

	/**
	 * 好友列表 操作信息枚举
	 * 
	 * @author 蔡琪
	 * 
	 */
	public enum Operation {
		/** 通过角色id获取该角色好友分组信息 */
		getFriendGroupsByRoleId,
		/** 删除好友分组信息 */
		deleteFriendGroup,
		/** 添加好友分组 */
		addFriendGroup
	}

	/** 角色ID */
	private Long roleID;

	/** 好友分组集合 */
	private List<FriendGroup> friendGroups = new ArrayList<FriendGroup>();

	/** 好友分组信息 */
	private FriendGroup friendGroup;

	private Operation operation;

	public Long getRoleID() {
		return roleID;
	}

	public void setRoleID(Long roleID) {
		this.roleID = roleID;
	}

	public List<FriendGroup> getFriendGroups() {
		return friendGroups;
	}

	public void setFriendGroups(List<FriendGroup> friendGroups) {
		this.friendGroups = friendGroups;
	}

	public FriendGroup getFriendGroup() {
		return friendGroup;
	}

	public void setFriendGroup(FriendGroup friendGroup) {
		this.friendGroup = friendGroup;
	}

	public Operation getOperation() {
		return operation;
	}

	public void setOperation(Operation operation) {
		this.operation = operation;
	}

	public String getChildElementXML() {
		StringBuffer xml = new StringBuffer();
		xml.append("<query xmlns=\"jabber:iq:friendGroup\">");
		xml.append("<friendGroups>");
		for (FriendGroup friendGroup : friendGroups) {
			xml.append(createFriendGroupNode(friendGroup));
		}
		xml.append("</friendGroups>");
		if (operation != null && !operation.equals("")) {
			xml.append("<operation>");
			xml.append(operation);
			xml.append("</operation>");
		}
		if (friendGroup != null) {
			xml.append(createFriendGroupNode(friendGroup));
		}
		if (roleID != null) {
			xml.append("<roleID>");
			xml.append(roleID);
			xml.append("</roleID>");
		}
		xml.append("</query>");
		return xml.toString().replaceAll("\\>\\s*|\t|\r|\n", ">");
	}

	/**
	 * 生成friendGroup节点
	 * 
	 * @param friendGroup
	 * @return
	 */
	private String createFriendGroupNode(FriendGroup friendGroup) {
		return "<friendGroup>" + StringUtils.escapeNode(friendGroup.toXML())
				+ ("</friendGroup>");
	}
}
